<?php
namespace Home\Controller;
use Think\Controller;

class CompanyController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 $this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}


	public function lst(){

		$usrinf = M('abtus');
		if(IS_POST){
			$data=I('post.');
			$data['create_time']=date('Y-m-d H:i:s');
			if($data['id']){
				$addData = $usrinf->save($data);
				if($addData!==false){
					$this->success('更新成功','lst');
				}else{
					$this->error('服务器忙，稍等....');
				}
			}else{  //新增 无ID
				$addData = $usrinf->add($data);
				if($addData){
					$this->success('设置成功','lst');
				}else{
					$this->error('服务器忙，稍等....');
				}
			}
			
			
			
		}// get 方式打开
		
		$res = $usrinf->find(1);
		$res['contact'] = html_entity_decode($res['contact']);
		$this->assign('res',$res);
		$this->display();
	}

	

	public function contact(){

		if(IS_POST){  //修改联系信息时
			$chk_para = I('post.','','htmlspecialchars');

			if($chk_para['c_name']){
				//$sql .= "where c_name like '%".$chk_para['c_name']."%' ";
				$where['c_name'] = array('like','%'.$chk_para['c_name'].'%');
			}

			if($chk_para['up_time_start'] && $chk_para['up_time_end']){
				$where['c_time'] = array(array('gt',$chk_para['up_time_start']) ,array('lt',$chk_para['up_time_end']."23:59:59"));
			}

			if($chk_para['c_belongs']){
				$where['c_belongs'] = array('eq',$chk_para['c_belongs']);
			}


			//$this->display();

		}
		
		$s = M('client_info');
		$belongs_info = $s->field('c_belongs')->group('c_belongs')->select();
		$this->assign('belongs_info',$belongs_info);

		if($where){
			$search_data = $s->where($where)->order('id desc')->select();
			$this->assign('search_data',$search_data);
		}
		
		$this->display();
	}





	public function add(){


		if(IS_POST){

			$add_client = I('post.','','htmlspecialchars');
			

			//dump($add_client);
			
			$res = M('client_info')->add($add_client);
			//echo M('client_info')->getLastSql();die;

			if($res){
				$this->success('OK','lst');
			}else{
				$this->error('Fail');
			}

		}

		

		$this->display();
	}


}